package com.mymarkdown.app.config;

import com.mymarkdown.app.common.constant.Constant;
import com.mymarkdown.app.common.lister.FileWatch;
import com.mymarkdown.app.common.lister.WatchFile;
import com.mymarkdown.app.common.lister.impl.FileWatchImpl;
import com.mymarkdown.app.factory.FileWatchFactory;
import com.mymarkdown.app.util.CurrentUserUtil;
import com.mymarkdown.app.util.TemplateHtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zenglun
 * @date 2021/12/8 9:19 上午
 * @description: TODO
 */
@Configuration
public class ApplicationBeanConfig {

    @Autowired
    private CacheManager cacheManager;


    @Autowired
    private List<WatchFile> watchFiles;

    @Bean
    public TemplateHtmlUtil templateHtmlUtil(){
        Cache cache = cacheManager.getCache(Constant.AllCacheKey.CACHE_HTML_TEMPLATE_KEY);
        TemplateHtmlUtil templateHtmlUtil = TemplateHtmlUtil.getSelf();
        templateHtmlUtil.setCache(cache);
        return templateHtmlUtil;
    }

    @Bean
    public CurrentUserUtil currentUserUtil(){
        return CurrentUserUtil.getSelf();
    }

    @Bean
    public FileWatch<String> fileWatch(){
        FileWatch<String> fileWatch = FileWatchFactory.<String>getFileWatch();
        Optional.ofNullable(watchFiles).orElseGet(ArrayList::new).forEach(fileWatch::addWatch);
        return fileWatch;
    }


}
